/**
 * 
 */
package com.lastct.controller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.naming.NamingException;

import com.lastct.connection.ConnectionUtil;
import com.lastct.controller.interfaces.OfferManager;
import com.lastct.dao.Offer;
import com.lastct.dao.OfferTypes;

/**
 * @author Marco DC
 *
 */
public class OfferManagerManagerConcrete implements OfferManager{

	@Override
	public Offer[] getOffers() {
		ArrayList<Offer> result = new ArrayList<Offer>();
		Connection connection = null;
		try {
			connection = ConnectionUtil.getConnectionDB();
			String query = "SELECT idoffers, name, description, offer, time, image FROM offers";
			
			PreparedStatement ps = connection.prepareStatement(query);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				// Read values using column name
				int id = rs.getInt("idoffers");
				String name = rs.getString("name");
				String description = rs.getString("description");
				String offer = rs.getString("offer");
				String image = rs.getString("image");
				long time = rs.getLong("time");
				
				Offer offTmp = new Offer();
				offTmp.setId(id);
				offTmp.setName(name);
				offTmp.description = description;
				offTmp.image = image;
				offTmp.time = time;
				offTmp.offer = offer;
				result.add(offTmp);				
			}
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Offer[] offlist = new Offer[result.size()]; 
				result.toArray(offlist);
		return result.toArray(offlist);
	}
	
	@Override
	public Offer[] getOffersByType(String type) {
		ArrayList<Offer> result = new ArrayList<Offer>();
		Connection connection = null;
		try {
			connection = ConnectionUtil.getConnectionDB();
			String query = "SELECT idoffers, name, description, offer, time, image FROM offers where offer_type = ? ";
			
			PreparedStatement ps = connection.prepareStatement(query);
			ps.setString(1, type);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				// Read values using column name
				int id = rs.getInt("idoffers");
				String name = rs.getString("name");
				String description = rs.getString("description");
				String offer = rs.getString("offer");
				String image = rs.getString("image");
				long time = rs.getLong("time");
				
				Offer offTmp = new Offer();
				offTmp.setId(id);
				offTmp.setName(name);
				offTmp.description = description;
				offTmp.image = image;
				offTmp.time = time;
				offTmp.offer = offer;
				result.add(offTmp);				
			}
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Offer[] offlist = new Offer[result.size()]; 
		result.toArray(offlist);
		return result.toArray(offlist);
	}

	
	
	@Override
	public OfferTypes[] getOfferTypes() {
		ArrayList<OfferTypes> result = new ArrayList<OfferTypes>();
		Connection connection = null;
		try {
			connection = ConnectionUtil.getConnectionDB();
			String query = "SELECT id, description, type, image FROM offer_types";
			
			PreparedStatement ps = connection.prepareStatement(query);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				// Read values using column name
				int id = rs.getInt("id");
				String description = rs.getString("description");
				String type = rs.getString("type");
				String image = rs.getString("image");
				
				OfferTypes offTmp = new OfferTypes();
				offTmp.setId(id);
				offTmp.setDescription(description);
				offTmp.setImage(image);
				offTmp.setType(type);
				result.add(offTmp);				
			}
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		OfferTypes[] offlist = new OfferTypes[result.size()]; 
				result.toArray(offlist);
		return result.toArray(offlist);
	}



	@Override
	public Offer getOffer(String offerId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insert(Offer newOffer) {
		// TODO Auto-generated method stub
		
	}

}
